import { Point, point } from './Point';
import { Vector } from './Vector';

/**
 * A rectangle is defined by two points, the top left point, called topLeft,
 * and the bottom right point called bottomRight.
 */
export class Rectangle {
  constructor(public topLeft: Point, public bottomRight: Point) {
    if (topLeft.x > bottomRight.x || topLeft.y > bottomRight.y) {
      throw Error(`${topLeft} must be top left compared to ${bottomRight}`);
    }
  }

  toString(): string {
    return `tl: ${this.topLeft}, br: ${this.bottomRight}`;
  }

  /**
   * Check if there is an overlap between this and another rectangle.
   */
  overlap(other: Rectangle): Boolean {
    if (this.topLeft.x >= other.bottomRight.x || this.bottomRight.x <= other.topLeft.x) {
      return false;
    }
    if (this.topLeft.y >= other.bottomRight.y || this.bottomRight.y <= other.topLeft.y) {
      return false;
    }
    return true;
  }
}

/**
 * Function that constructs a rectangle from the two given points.
 */
export function rectangle(x1:number, y1: number, x2:number, y2:number): Rectangle {
  return new Rectangle(new Point(x1, y1), new Point(x2, y2));
}
